package com.own.business.system.menu.controller.manage;

import com.own.aop.permission.PermissionModule;
import com.own.aop.permission.PermissionOperation;
import com.own.business.system.menu.business.service.SystemRoleService;
import com.own.business.system.menu.common.constant.ConstantModule;
import com.own.business.system.menu.entity.dto.SystemRoleDto;
import com.own.business.system.menu.entity.map.SystemRoleMapVo;
import com.own.business.system.menu.entity.query.SystemRoleQuery;
import com.own.business.system.menu.entity.vo.SystemRoleVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.constant.ConstantPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ManageSystemRoleController
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Tag(name = "系统角色" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@PermissionModule(ConstantModule.ROLE)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.MANAGE_CLIENT + "system/role")
public class ManageSystemRoleController extends BaseController {

    @Resource
    private SystemRoleService systemRoleService;

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "查询所有信息")
    @PostMapping("/all")
    public ResultModel<List<SystemRoleVo>> all(
            @RequestBody SystemRoleQuery query
    ) {
        var list = systemRoleService.all(query);
        return R.success(list);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "分页查询信息")
    @PostMapping("/page")
    public ResultModel<PageModel<SystemRoleVo>> page(
            @RequestBody SystemRoleQuery query
    ) {
        var pageModel = systemRoleService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "map查询信息")
    @PostMapping("/map")
    public ResultModel<List<SystemRoleMapVo>> map(
            @RequestBody SystemRoleQuery query
    ) {
        var list = systemRoleService.map(query);
        return R.success(list);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.ADD)
    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<SystemRoleVo> add(
            @Validated @RequestBody SystemRoleDto dto
    ) {
        dto.check();
        var updateModel = systemRoleService.add(dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改信息")
    @PutMapping("/{roleId}")
    public ResultModel<SystemRoleVo> update(
            @Parameter(description = "主键id", required = true) @PathVariable Long roleId,
            @Validated @RequestBody SystemRoleDto dto
    ) {
        dto.check();
        var updateModel = systemRoleService.update(roleId, dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.DELETE)
    @ConfirmAop(message = "确认删除该项目？（注意：删除之后无法找回数据，且该项目所有资料会被删除）")
    @Operation(summary = "删除信息")
    @DeleteMapping("/{roleId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long roleId
    ) {
        var updateModel = systemRoleService.delete(roleId);
        return R.success(updateModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.VIEW)
    @Operation(summary = "根据id获取信息")
    @GetMapping("/{roleId}")
    public ResultModel<SystemRoleVo> getById(
            @Parameter(description = "主键id", required = true) @PathVariable Long roleId
    ) {
        var vo = systemRoleService.getById(roleId);
        return R.success(vo);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "修改角色的禁用状态")
    @PutMapping("/disable/{roleId}/{disable}")
    public ResultModel<Boolean> updateDisable(
            @Parameter(description = "主键id", required = true) @PathVariable Long roleId,
            @Parameter(description = "禁用状态 0-启用 1-禁用", required = true) @PathVariable Integer disable
    ) {
        systemRoleService.updateDisableById(roleId, disable);
        return R.success();
    }

}
